package com.zzl.crud.service;

import com.zzl.crud.bean.Employee;
import com.zzl.crud.bean.EmployeeExample;
import com.zzl.crud.bean.EmployeeExample.Criteria;
import com.zzl.crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    /**
     * 检查用户是否可用
     * 可用 返回 true
     * @param empName
     * @return
     */
    public boolean checkUser(String empName) {

        EmployeeExample employeeExample = new EmployeeExample();
        Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(employeeExample);
        return count == 0;
    }

    /**
     * 查询所有员工
     * @return
     */
    public List<Employee> getAll() {
        return employeeMapper.selectByExampleWithDept(null);
    }

    /**
     * 添加员工
     * @param employee
     */
    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    public Employee getEmp(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public void delEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void delEmpAll(List<Integer> listId) {
        EmployeeExample employeeExample = new EmployeeExample();
        Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpIdIn(listId);
        employeeMapper.deleteByExample(employeeExample);
    }
}
